﻿
layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer;

    $(function () {
        loadList();
    });
});

//查询
function search() {
    var queryParams = {
        "s_0_UserAccount|UserName|Phone": $('#tbKey').val(),
        "s_1_UserType": $('#tbUserType').val()
    };
    $.extend(true, _table.InitOptions.params, queryParams); //分页带查询参数
    _table.reload();
}

//初始化表格
function loadList() {
    _table.init({
        elem: '#table',
        url: 'UserInfoManage.ashx',
        contentType: "application/x-www-form-urlencoded", /**支持跨域**/
        pageSize: 15, //单页记录数
        striped: false, //是否显示行间隔色
        checkboxHeader: true,
        singleSelect: false,
        onLoadSuccess: function (data) {
        },
        params: {
            PostType: 'list',
            ActionType: 'UserInfo',
            rmd: Math.random()
        }
    });
}

//格式化序号
function formatterNum(value, row, index) {
    //获取每页显示的数量
    var pageSize = $('#table').bootstrapTable('getOptions').pageSize;
    //获取当前是第几页
    var pageNumber = $('#table').bootstrapTable('getOptions').pageNumber;
    //返回序号，注意index是从0开始的，所以要加上1
    return (pageSize * (pageNumber - 1) + index + 1);
}

//格式化性别
function formatterSex(value, row, index) {
    if (value == 1) {
        return '男';
    } else {
        return '女';
    }
}

//格式化账号类型
function formatterUserType(value, row, index) {
    var acthtml = '';
    switch (value) {
        case 2:
            acthtml = '系统管理员';
            break;
        case 3:
            acthtml = '普通账号';
            break;
        case 4:
            acthtml = '测试账号';
            break;
    }
    return acthtml;
}

//格式化状态
function formatterIsLock(value, row, index) {
    if (value == 1) {
        return '<span class="dangerOutSpan">失效</span>';
    } else {
        return '<span class="successOutSpan">正常</span>';
    }
}

//格式化操作按钮
function formatterOperationMenu(value, row, index) {
    var acthtml = '';
    var opHtml = $('#bst_tableEvent').html();
    if (opHtml) {
        var eventJson = $.parseJSON(opHtml);
        $.each(eventJson, function (idx, item) {
            if (item.MenuName == '分配权限') {
                acthtml += '<br><a class="btn-' + item.MenuEvent + '" onclick="' + item.MenuEvent + '(\'' + row["GUID"] + '\',\'' + row["UserName"] + '\')">' + item.MenuName + '</a>';
            } else {
                acthtml += '<a class="btn-' + item.MenuEvent + '" onclick="' + item.MenuEvent + '(\'' + row["GUID"] + '\')">' + item.MenuName + '</a>';
            }
        });
    }
    return acthtml;
}

//新增
function add() {
    layer.open({
        title: "账号管理",
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "UserInfoSet.aspx"
    });
}

//查看
function view(id) {
    layer.open({
        title: "账号管理",
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "UserInfoView.aspx?GUID=" + id
    });
}

//编辑
function edit(id) {
    layer.open({
        title: "账号管理",
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "UserInfoSet.aspx?GUID=" + id
    });
}

//删除
function del(id) {
    layer.confirm('确定删除?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        app.del('UserInfoManage.ashx', {
            ActionType: 'UserInfo',
            GUID: id
        }, function (data) {
            layer.msg(data.msg);
            if (data.success) {
                search();
            }
        });
        layer.close(index);
    });
}

//重置密码
function resetPwd(id) {
    var defaultPwd = $('#tbDefaultPwd').val();
    layer.confirm('确定要重置密码【' + defaultPwd + '】?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        app.set('UserInfoManage.ashx', {
            ActionType: 'ResetPwd',
            GUID: id
        }, function (data) {
            layer.msg(data.msg);
        });
        layer.close(index);
    });
}

//设置密码
function updatePwd(id) {
    layer.prompt({
        formType: 0,
        title: '设置密码',
    }, function (value, index, elem) {
        var userPwd = value;
        console.log(IsComplexPwd(userPwd));
        if (!IsComplexPwd(userPwd)) {
            layer.msg("密码必须大于8位且要包含大小写字母、数字！", { icon: 5 });
            return;
        }
        app.set('UserInfoManage.ashx', {
            ActionType: 'UpdatePwd',
            GUID: id,
            UserPwd: userPwd
        }, function (data) {
            layer.msg(data.msg);
            layer.close(index);
        });
    });
}

//分配权限
function allotPermission(id, userName) {
    layer.open({
        title: "分配权限-" + userName,
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "/Business/RoleManage/UserPermissionSet.aspx?GUID=" + id
    });
}
